package com.hongyun.tms.web.controller;

import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.RepeatSubmit;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.tug.TmsMTugQueryDto;
import com.hongyun.tms.common.dto.tug.TmsMTugSaveReqDto;
import com.hongyun.tms.common.dto.tug.TugImportDto;
import com.hongyun.tms.common.dto.tug.TugTemplateDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.TmsMTugVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.entity.TmsMTug;
import com.hongyun.tms.infra.excel.EasyExcelUtil;
import com.hongyun.tms.infra.excel.listener.TugInfoExcelListener;
import com.hongyun.tms.service.TmsMTugService;
import com.hongyun.tms.web.utils.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
* 拖轮信息表
*
* @author yuheng
* @since 1.0.0 2024-06-06
*/
@RestController
@RequestMapping("basicInfo/tug")
@Api(value = "拖轮信息", tags = "拖轮信息")
@AllArgsConstructor
public class TugController {
    private final TmsMTugService tmsMTugService;

    @GetMapping("page")
    @ApiOperation(value = "分页", notes = "分页")
    public AjaxResult<PageResponseDto<TmsMTugVO>> page(TmsMTugQueryDto query){
        PageResponseDto<TmsMTugVO> page = tmsMTugService.page(query);
        return AjaxResult.success(page);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "详情", notes = "id")
    public AjaxResult<TmsMTugVO> get(@PathVariable("id") Long id){
        TmsMTug entity = tmsMTugService.getById(id);
        return AjaxResult.success(MapperingUtils.copyProperties(entity, TmsMTugVO.class));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "传入TmsMTug")
    @RepeatSubmit
    public AjaxResult save(@RequestBody @Valid TmsMTugSaveReqDto vo){
         tmsMTugService.save(vo);
         return AjaxResult.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新")
    @RepeatSubmit
    public AjaxResult update(@RequestBody @Valid TmsMTugSaveReqDto dto){
        tmsMTugService.update(dto);
        return AjaxResult.success();
    }

    @PostMapping("/remove")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    @RepeatSubmit
    public AjaxResult delete(@ApiParam(value = "主键集合", required = true) @RequestParam(name = "ids") String ids){
        tmsMTugService.deleteBatch(ids);
        return AjaxResult.success();
    }

    @PostMapping("/importTug")
    @ApiOperation(value = "导入")
    public AjaxResult<Void> importTug(MultipartFile file) throws IOException {
        AjaxResult<Void> validationResult = FileUploadUtil.validateExcelFile(file);
        // 根据code判断校验是否通过
        if (validationResult.getCode() != HttpStatus.OK.value()) {
            return validationResult;
        }

        // 创建 Excel 数据处理监听器
        TugInfoExcelListener listener = new TugInfoExcelListener(tmsMTugService);

        // 处理 Excel 文件
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcelUtil.syncReadModel(inputStream, listener, TugImportDto.class, 0, 1);
            return AjaxResult.success();
        }
    }

    @GetMapping("exportTemplate")
    @ApiOperation(value = "导出模板")
    @ExcelExport(value = TugTemplateDto.class)
    public AjaxResult exportTemplate(HttpServletResponse response) {
        return AjaxResult.success(Collections.emptyList());
    }

    @GetMapping("exportData")
    @ApiOperation(value = "导出数据")
    @ExcelExport(value = TmsMTugVO.class)
    public AjaxResult<List<TmsMTugVO>> exportData(TmsMTugQueryDto query, HttpServletResponse response) {
        List<TmsMTugVO> exportList = tmsMTugService.export(query);
        return AjaxResult.success(exportList);
    }
}
